package org.zsk.cfl.listerner;

import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.flowable.task.service.delegate.DelegateTask;
import org.flowable.task.service.delegate.TaskListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 * </p>
 *
 * @author zsk
 * @date 2020/11/7 下午4:10
 */
@Component
public class MultiAssignListener implements TaskListener, ApplicationContextAware {

    /**
     * 日志类
     */
    private static final Logger log = LoggerFactory.getLogger(MultiAssignListener.class);
    private static ApplicationContext applicationContext;


    @Override
    public void notify(DelegateTask delegateTask) {
        log.warn("进入会签用户任务启动监听器");

//        log.warn("会签候选人："+delegateTask.getCandidates().size());
        delegateTask.getCandidates().forEach(identityLink -> {
            log.info("候选人名称："+identityLink.getUserId());
        });
        TaskService taskService=applicationContext.getBean(TaskService.class);
        List<Task> tasks=taskService.createTaskQuery().processInstanceId(delegateTask.getProcessInstanceId())
                .processDefinitionId(delegateTask.getProcessDefinitionId()).list();
        tasks.forEach(task -> {
            log.info("会签任务Id："+task.getId());
        });
//        List<String> assignees=delegateTask.getCandidates().stream().map(IdentityLinkInfo::getUserId).collect(Collectors.toList());
//        delegateTask.setVariable("num",assignees.size());
//        delegateTask.setVariable("assignees",assignees);

        log.warn("退出通用用户任务启动监听器");
    }

    @Override
    public void setApplicationContext(ApplicationContext agr0) throws BeansException {
        applicationContext=agr0;
    }
}
